package com.record.controller;


import com.record.dto.AnalysisResultDto;
import com.record.service.EccService;
import com.record.service.TasteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@Api(tags = "上传Excel分析数据")
@RestController
public class EccController {

    @Autowired
    private EccService eccService;


    @ApiOperation("上传Excel并返回统计结果（A1=统计分析；A2开始为被统计文本）")
    @PostMapping(value = "/analyze", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AnalysisResultDto analyze(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeExcel(file1);
    }


    @ApiOperation("上传Excel计算三组均值（最多三列）")
    @PostMapping("/analyzeGroupAverage")
    public Map<String, Object> analyzeGroupAverage(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeGroupAverage(file1);
    }



    @ApiOperation("上传Excel计算三组标准差（最多三列）")
    @PostMapping("/analyzeGroupStd")
    public Map<String, Object> analyzeGroupStd(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeGroupStd(file1);
    }


    @ApiOperation("上传Excel并计算每列的Shapiro-Wilk正态性检验（返回W值与P值）")
    @PostMapping("/analyzeShapiro")
    public Map<String, Object> analyzeShapiro(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeShapiro(file1);
    }



    @ApiOperation("上传Excel并进行方差齐性检验（返回F值与P值）")
    @PostMapping("/analyzeVariance")
    public Map<String, Object> analyzeVariance(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeVariance(file1);
    }


    @ApiOperation("上传Excel并进行单因素方差分析（最多15列，返回F值与P值）")
    @PostMapping("/analyzeAnova")
    public Map<String, Object> analyzeAnova(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeAnova(file1);
    }


    @ApiOperation("上传Excel并进行两两独立样本t检验（  最多三列）")
    @PostMapping("/analyzeTTest")
    public Map<String, Object> analyzeTTest(@RequestPart("file1") MultipartFile file1) throws Exception {
        return eccService.analyzeTTest(file1);
    }


    @ApiOperation("上传Excel并进行非参数检验（含Mann–Whitney与Kruskal–Wallis）")
    @PostMapping("/analyzeNonParametricFull")
    public Map<String, Object> analyzeNonParametricFull(@RequestPart("file1") MultipartFile file) throws Exception {
        return eccService.analyzeNonParametricFull(file);
    }


    @ApiOperation("线性回归分析（返回β₀、β₁、R²、p-value）")
    @PostMapping("/analyzeLinearRegression")
    public Map<String, Object> analyzeLinearRegression(@RequestPart("file1") MultipartFile file1)throws Exception {
        return eccService.analyzeLinearRegression(file1);
    }


    @ApiOperation("KMeans 聚类分析（不限行列）")
    @PostMapping("/cluster")
    public Map<String, Object> clusterAnalysis(@RequestPart("file1") MultipartFile file,
                                               @RequestParam(value = "k", required = false) Integer k) {
        return eccService.clusterAnalysis(file, k);
    }


    @ApiOperation("卡方检验 2列 3列 4列）")
    @PostMapping("/chi-square")
    public Map<String, Object> chiSquareTest(@RequestParam("file1") MultipartFile file,
                                             @RequestParam(value = "sheetIndex", required = false) Integer sheetIndex) {
        return eccService.chiSquareTest(file, sheetIndex);
    }



    /**
     * 上传Excel执行关联规则分析
     * @param file Excel文件
     * @param sheetIndex 工作表索引（默认0）
     * @return JSON结果
     */
    @PostMapping("/association-rule")
    //sheetIndex=0
    public Map<String, Object> analyzeAssociationRules(
            @RequestParam("file1") MultipartFile file,
            @RequestParam(value = "sheetIndex", required = false) Integer sheetIndex) {
        return eccService.analyzeAssociationRules(file, sheetIndex);
    }



}
